﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Utility;
using MyAlbum.DataAccess.LinqToSql;


namespace MyAlbum.Data.Linq
{
    /// <summary>
    /// Manages the replies for the entries. This is done separately from the entries 
    ///  repository as the replies are added and edited on different times and places and 
    ///  not when the entry is created/edited
    /// </summary>
    public class ReplyRepository : IRepository<MyAlbum.Model.Reply>
    {
        private string _connectionString;
        public ReplyRepository(string connectionString)
        {
            Assert.IsNotNull(connectionString);
            this._connectionString = connectionString;
        }
        #region IRepository<Entry> Members

        public List<MyAlbum.Model.Reply> GetItems()
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.Replies.Select(r =>
                    new MyAlbum.Model.Reply()
                    {
                        Body = r.Body,
                        Email = r.Email,
                        EntryId = r.EntryId.ToString(),
                        Id = r.Id.ToString(),
                        IsApproved = r.Approved,
                        Subject = r.Subject,
                        TimeStamp = r.Timestamp,
                        UserName = r.Username
                    }).ToList();
            }
        }

        public void AddItem(MyAlbum.Model.Reply item)
        {
            Assert.IsNotNull(item);
            Reply r = new Reply()
            {
                 Id=Guid.NewGuid(),
                Username = item.UserName,
                Timestamp = item.TimeStamp,
                Subject = item.Subject,
                Approved = item.IsApproved,
                Body = item.Body,
                EntryId =new Guid( item.EntryId),
                Email = item.Email
            };
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                ImagesDB.Replies.InsertOnSubmit(r);
                ImagesDB.SubmitChanges();
            }
            item.Id = r.Id.ToString();
        }

        public void DeleteItem(MyAlbum.Model.Reply item)
        {
            Assert.IsNotNull(item);
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var old = ImagesDB.Replies.FirstOrDefault(r => r.Id == new Guid(item.Id));
                if (old != null)
                {
                    ImagesDB.Replies.DeleteOnSubmit(old);
                    ImagesDB.SubmitChanges();
                }
            }

        }

        public void UpdateItem(MyAlbum.Model.Reply item)
        {
            Assert.IsNotNull(item);
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var old = ImagesDB.Replies.FirstOrDefault(r => r.Id == new Guid(item.Id));
                if (old != null)
                {
                    old.Username = item.UserName;
                    old.Subject = item.Subject;
                    old.Approved = item.IsApproved;
                    old.Body = item.Body;
                    old.EntryId = new Guid(item.EntryId);
                    old.Email = item.Email;
                    ImagesDB.SubmitChanges();
                }
            }
        }

        public MyAlbum.Model.Reply GetItemById(string id)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.Replies.Where(r => r.Id == new Guid(id)).Select(r =>
                    new MyAlbum.Model.Reply()
                    {
                        Body = r.Body,
                        Email = r.Email,
                        EntryId = r.EntryId.ToString(),
                        Id = r.Id.ToString(),
                        IsApproved = r.Approved,
                        Subject = r.Subject,
                        TimeStamp = r.Timestamp,
                        UserName = r.Username
                    }).SingleOrDefault();
            }
        }

        #endregion
    }
}
